<?php
// Tim'Roster
// Copyright (c) 2011 Nicolas Nallet <aspgic2@gmail.com>
//
// This file is part of Tim'Roster.
//
// Tim'Roster is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Tim'Roster is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

$xml_config = simplexml_load_file('config.xml');

include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/technical_class/authentification/User.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/technical_class/language/language.php');


class Mail_Sender {

    const SEPARATOR = "\r\n";

    private static function get_headers(){
        return 'From: Administration Tim\' Roster <noreply@'.$_SERVER['HTTP_HOST'].'>\r\n\r\n';
    }

    /**
     * (PHP 5)<br/>
     * Send a mail to the member of a group
     * @param int $id_group
     * @param string $suject
     * @param string $msg
     */
    private static function send_email_to_a_group($id_group, $suject, $msg){
        $user_array = User::get_user_list($id_group);

        if ((isset($user_array)) && (count($user_array) > 0)){
            for ($index = 0; $index < count($user_array); $index++) {
                if (!mail($user_array[$index]->get_email(), $suject, $msg, Mail_Sender::get_headers())){
                    //TODO : Log an error
                }
            }
        }
    }

    /**
     * (PHP 5)<br/>
     * Send a mail to one user
     * @param int $user_id
     * @param string $suject
     * @param string $msg
     */
    private static function send_email_to_user($user_id, $suject, $msg){
        $user = User::get_user($user_id);

        if (isset($user)){
            if (!mail($user->get_email(), $suject, $msg, Mail_Sender::get_headers())){
                    //TODO : Log an error
            }
        }
    }

    /**
     * (PHP 5)<br/>
     * Send a mail to the roster admins, when a new user register
     * @param int $new_user_id
     */
    public static function send_admin_new_user_register_mail($new_user_id){
        $new_user = User::get_user($new_user_id);

        if (isset($new_user)){
            $suject = Language::get_instance()->get_label('lb_new_user_register').' : '.ucfirst($new_user->get_name());

            $url = dirname('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'/user.php?id='.$new_user_id;

            $msg = Language::get_instance()->get_label('lb_hello').','.Mail_Sender::SEPARATOR.Mail_Sender::SEPARATOR
                    .Language::get_instance()->get_label('lb_new_user_register').'. '.Language::get_instance()->get_label('lb_click_here_to_see_profil').Mail_Sender::SEPARATOR
                    .$url.' '.Mail_Sender::SEPARATOR.Mail_Sender::SEPARATOR
                    .Language::get_instance()->get_label('lb_greeting').Mail_Sender::SEPARATOR.Mail_Sender::SEPARATOR
                    .'Tim\' Roster - Ryzom';

            Mail_Sender::send_email_to_a_group(User::ADMIN_GROUP_ID, $suject, $msg);
        }
    }

    /**
     * (PHP 5)<br/>
     * Send a mail with the activation link 
     * @param int $new_user_id
     */
    public static function send_user_activation_mail($new_user_id){
        $new_user = User::get_user($new_user_id);

        if (isset($new_user)){
            $suject = Language::get_instance()->get_label('lb_activation_mail_subject').' : '.ucfirst($new_user->get_name());

            $url = dirname('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'/activate.php?user='.$new_user->get_name().'&email='.$new_user->get_email()
                    .'&act='.$new_user->get_activation_token();

            $msg = Language::get_instance()->get_label('lb_hello').','.Mail_Sender::SEPARATOR.Mail_Sender::SEPARATOR
                    .Language::get_instance()->get_label('lb_thank_you_for_register').' ('.$_SERVER['HTTP_HOST'].').'.Mail_Sender::SEPARATOR.Mail_Sender::SEPARATOR
                    .Language::get_instance()->get_label('lb_activation_mail1').Mail_Sender::SEPARATOR
                    .$url.Mail_Sender::SEPARATOR.Mail_Sender::SEPARATOR
                    .Language::get_instance()->get_label('lb_activation_mail2').Mail_Sender::SEPARATOR.Mail_Sender::SEPARATOR
                    .Language::get_instance()->get_label('lb_greeting').Mail_Sender::SEPARATOR.Mail_Sender::SEPARATOR
                    .'Tim\' Roster - Ryzom';

            Mail_Sender::send_email_to_user($new_user_id, $suject, $msg);
        }
    }

    /**
     * (PHP 5)<br/>
     * Send a mail with the new password
     * @param int $user_id
     */
    public static function send_user_password_recovery_mail($user_id, $new_password){
        $user = User::get_user($user_id);

        if ((isset($user)) && (isset($new_password))){
            $suject = Language::get_instance()->get_label('lb_password_recovery_for_user').' : '.ucfirst($user->get_name());

            $msg = Language::get_instance()->get_label('lb_hello').','.Mail_Sender::SEPARATOR.Mail_Sender::SEPARATOR
                    .Language::get_instance()->get_label('lb_request_password_recovery').Mail_Sender::SEPARATOR
                    .Language::get_instance()->get_label('lb_password').' : '.$new_password.Mail_Sender::SEPARATOR.Mail_Sender::SEPARATOR
                    .Language::get_instance()->get_label('lb_greeting').Mail_Sender::SEPARATOR.Mail_Sender::SEPARATOR
                    .'Tim\' Roster - Ryzom';

            Mail_Sender::send_email_to_user($user_id, $suject, $msg);
        }
    }
}


?>
